/********************************************************************************
 * UserLoginAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Aug 26, 2008 10:57:46 AM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/

package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import org.hibernate.criterion.Expression;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.I18nInterceptor;
import com.printeverywhere.delegator.CommonFunctionDelegator;

import com.paypal.soap.api.ItemType;
import com.printeverywhere.delegator.AuctionBidsServiceDelegator;
import com.printeverywhere.delegator.AuctionServiceDelegator;
import com.printeverywhere.delegator.ItemsServiceDelegator;
import com.printeverywhere.delegator.ItemsWatchListServiceDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.AuctionBids;
import com.printeverywhere.dto.AuctionLostlist;
import com.printeverywhere.dto.AuctionWonlist;
import com.printeverywhere.dto.BidStatus;
import com.printeverywhere.dto.ItemOffers;
import com.printeverywhere.dto.ItemWantedBestOffer;
import com.printeverywhere.dto.Items;
import com.printeverywhere.dto.ItemsWatchlist;
import com.printeverywhere.dto.Itemtype;
import com.printeverywhere.dto.MailingSystem;
import com.printeverywhere.dto.MyAccountCount;
import com.printeverywhere.dto.User;
import com.printeverywhere.dto.UserFavoritesCategory;
import com.printeverywhere.dto.UserFavoritesCompanyprofile;
import com.printeverywhere.dto.UserFavoritesSearch;
import com.printeverywhere.dto.UserFavoritesShops;
import com.printeverywhere.dto.UserFavoritesUsers;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.CommonUtil;
import com.printeverywhere.util.DateUtil;
import com.printeverywhere.util.MailHelper;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SearchListCriteria;
import com.printeverywhere.util.SessionUtil;

/**
 * The Class UserLoginAction.
 */
public class UserLoginAction extends FrontBaseSupport implements
		ServletRequestAware, ServletResponseAware {

	public UserLoginAction() {
		super(true, false);
		setLoginPage(true);
	}

	/** Logger for this class. */
	private static final Logger logger = Logger
			.getLogger(UserLoginAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The email. */
	private String email;

	/** The user id. */
	private String userId;

	/** The message. */
	private boolean message;

	private boolean remember;

	private boolean forget;

	private String redirectUri;

	/** The request. */
	private HttpServletRequest request;

	private HttpServletResponse response;

	private MyAccountCount myAccountCount = new MyAccountCount();

	/*
	 * (non-Javadoc)
	 *
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.setAttribute(AttributeConstants.USER_ID, userName);
		searchCriteria.setAttribute(AttributeConstants.PASSWORD, password);
		List<User> userList = UserServiceDelegator.getList(searchCriteria);

		if (userList != null && userList.isEmpty() == false
				&& userList.get(0).getPassword().equals(password)) {
			User user = userList.get(0);
			if(!user.getActive() && user.getActivationKey() != null) {
				addActionError("Your account is Pending.");
			}
			else if (user.getActive()) {
				Map<Object, Object> session = ActionContext.getContext()
						.getSession();
				session.put(AttributeConstants.USER, user);
				request.getSession().setAttribute(AdminConstants.TEXT,user.getLanguages().getText());
				request.getSession().setAttribute(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE, new Locale(user.getLanguages().getLanguageCode()));

				List<Criterion> criterionList = new ArrayList<Criterion>();
				criterionList.add(Restrictions.eq("user", user));

				myAccountCount.setFavoriteUsers(CommonFunctionDelegator.getCountByCriterion(criterionList, UserFavoritesUsers.class));
				myAccountCount.setFavoriteCategories(CommonFunctionDelegator.getCountByCriterion(criterionList, UserFavoritesCategory.class));
				myAccountCount.setFavoriteCompany(CommonFunctionDelegator.getCountByCriterion(criterionList, UserFavoritesCompanyprofile.class));
				myAccountCount.setFavoriteSearches(CommonFunctionDelegator.getCountByCriterion(criterionList, UserFavoritesSearch.class));
				myAccountCount.setFavoriteShops(CommonFunctionDelegator.getCountByCriterion(criterionList, UserFavoritesShops.class));

				/*myAccountCount.setSellingAuction(ItemsServiceDelegator.getSellingSize(user, AttributeConstants.ITEMTYPE_STRAIGHTAUCTION));
				myAccountCount.setSellingFixedPrice(ItemsServiceDelegator.getSellingSize(user, AttributeConstants.ITEMTYPE_FIXEDPRICE));
				myAccountCount.setSellingShops(ItemsServiceDelegator.getSellingSize(user, AttributeConstants.ITEMTYPE_SHOPS));
				//myAccountCount.setSellingSold(ItemsServiceDelegator.getSoldItems(user).size());
				myAccountCount.setSellingNotSold(ItemsServiceDelegator.getNotSoldItems(user).size());
				*/
//				request.getSession().setAttribute(AttributeConstants.MYACCOUNTCOUNT, myAccountCount);

				int count = 0;
				criterionList = new ArrayList<Criterion>();
			    Itemtype itemType = (Itemtype) CommonFunctionDelegator.getObjectById(AdminConstants.ID, AttributeConstants.ITEMTYPE_STRAIGHTAUCTION, Itemtype.class);
				criterionList.add(Restrictions.eq(AdminConstants.USERID , user));
				criterionList.add(Restrictions.eq(AttributeConstants.ITEMTYPE, itemType));
				criterionList.add(Expression.or(Restrictions.ne("status", new BidStatus(5)), Restrictions.ne("status", new BidStatus(1))));
				criterionList.add(Restrictions.ge("endTime", DateUtil.getTodaysDate()));
			    count = CommonFunctionDelegator.getCountByCriterion(criterionList, Items.class);

			    myAccountCount.setSellingAuction(count);

			    itemType = new Itemtype();
			    if(criterionList.size() > 0 ){
			    	criterionList.clear();
			    }
			    itemType = (Itemtype) CommonFunctionDelegator.getObjectById(AdminConstants.ID, AttributeConstants.ITEMTYPE_FIXEDPRICE, Itemtype.class);
				criterionList.add(Restrictions.eq(AdminConstants.USERID , user));
				criterionList.add(Restrictions.eq(AttributeConstants.ITEMTYPE, itemType));
				criterionList.add(Expression.or(Restrictions.ne("status", new BidStatus(5)), Restrictions.ne("status", new BidStatus(1))));
				criterionList.add(Restrictions.ge("endTime", DateUtil.getTodaysDate()));
			    count = CommonFunctionDelegator.getCountByCriterion(criterionList, Items.class);
				myAccountCount.setSellingFixedPrice(count);

				itemType = new Itemtype();
			    if(criterionList.size() > 0 ){
			    	criterionList.clear();
			    }
			    itemType = (Itemtype) CommonFunctionDelegator.getObjectById(AdminConstants.ID, AttributeConstants.ITEMTYPE_SHOPS, Itemtype.class);
				criterionList.add(Restrictions.eq(AdminConstants.USERID , user));
				criterionList.add(Restrictions.eq(AttributeConstants.ITEMTYPE, itemType));
				criterionList.add(Expression.or(Restrictions.ne("status", new BidStatus(5)), Restrictions.ne("status", new BidStatus(1))));
				criterionList.add(Restrictions.ge("endTime", DateUtil.getTodaysDate()));
			    count = CommonFunctionDelegator.getCountByCriterion(criterionList, Items.class);
				myAccountCount.setSellingShops(count);

				if(criterionList.size() > 0 ){
			    	criterionList.clear();
			    }
				criterionList.add(Restrictions.or(Restrictions.eq(AttributeConstants.ITEMTYPE_ID, AdminConstants.ONE_NUMBER), Restrictions.or(Restrictions.eq(AttributeConstants.ITEMTYPE_ID, AdminConstants.TWO_NUMBER), Restrictions.eq(AttributeConstants.ITEMTYPE_ID, AdminConstants.THREE_NUMBER))));
				criterionList.add(Restrictions.gt(AttributeConstants.SOLD, AdminConstants.ZERO_NUMBER));
				criterionList.add(Restrictions.eq(AttributeConstants.USER_ID, user));
				criterionList.add(Expression.lt("endTime", DateUtil.getTodaysDate()));
				myAccountCount.setSellingSold(CommonFunctionDelegator.getCountByCriterion(criterionList, Items.class));
				if(criterionList.size() > 0 ){
			    	criterionList.clear();
			    }

				criterionList.add(Restrictions.eq(AttributeConstants.USER, user));

				myAccountCount.setBuyingWatching(CommonFunctionDelegator.getCountByCriterion(criterionList, ItemsWatchlist.class));

				myAccountCount.setBuyingBidding(CommonFunctionDelegator.getCountByCriterion(criterionList, AuctionBids.class));

				myAccountCount.setBuyingPurchased(CommonFunctionDelegator.getCountByCriterion(criterionList, AuctionWonlist.class));

				myAccountCount.setBuyingLost(CommonFunctionDelegator.getCountByCriterion(criterionList, AuctionLostlist.class));

				if(criterionList.size() > 0 ){
			    	criterionList.clear();
			    }
				criterionList.add(Restrictions.or(Restrictions.eq(AttributeConstants.ITEMTYPE_ID, AdminConstants.ONE_NUMBER), Restrictions.or(Restrictions.eq(AttributeConstants.ITEMTYPE_ID, AdminConstants.TWO_NUMBER), Restrictions.eq(AttributeConstants.ITEMTYPE_ID, AdminConstants.THREE_NUMBER))));
				criterionList.add(Restrictions.eq(AttributeConstants.SOLD, AdminConstants.ZERO_NUMBER));
				criterionList.add(Restrictions.eq(AttributeConstants.USER_ID, user));
				criterionList.add(Expression.lt("endTime", DateUtil.getTodaysDate()));
				myAccountCount.setSellingNotSold(CommonFunctionDelegator.getCountByCriterion(criterionList, Items.class));

				if(criterionList.size() > 0 ){
			    	criterionList.clear();
			    }
				criterionList.add(Restrictions.eq("user.id", user.getId()));
				criterionList.add(Restrictions.eq("status.id", 4));
			    count = CommonFunctionDelegator.getCountByCriterion(criterionList, ItemOffers.class);
				myAccountCount.setBuyingMyOffers(count);

				if(criterionList.size() > 0 ){
			    	criterionList.clear();
			    }
			    itemType = (Itemtype) CommonFunctionDelegator.getObjectById(AdminConstants.ID, 5, Itemtype.class);
				criterionList.add(Restrictions.eq(AdminConstants.USERID , user));
				criterionList.add(Restrictions.eq(AttributeConstants.ITEMTYPE, itemType));
			    count = CommonFunctionDelegator.getCountByCriterion(criterionList, Items.class);
				myAccountCount.setMyAdvertisement(count);

				SearchListCriteria searchListCriteria = new SearchListCriteria();
				searchListCriteria.removeAllAttribute();
				searchListCriteria.setAttribute(AttributeConstants.USER, new Object[]{user, SearchListCriteria.EQ});
				List<ItemsWatchlist> itemsWatchList = ItemsWatchListServiceDelegator.getList(searchListCriteria);
				List<Items> itemsList = new ArrayList<Items>();
				for (Iterator<ItemsWatchlist> iterator = itemsWatchList.iterator(); iterator
						.hasNext();) {
					ItemsWatchlist itemsWatchlistDTO = (ItemsWatchlist) iterator
							.next();
					Items items = itemsWatchlistDTO.getItems();
					if((items.getUserId() != user) && (items.getItemtype().getId() == 5)){
						itemsList.add(items);
					}

				}
				if(itemsList.size() > 0 ){
					myAccountCount.setAdvertisingWatchingOthers(itemsList.size());
				}

				if(criterionList.size() > 0 ){
			    	criterionList.clear();
			    }
				count = 0;
				User loginUser = SessionUtil.getUser(request);
				criterionList.add(Restrictions.eq(AdminConstants.USERID , loginUser));
				criterionList.add(Restrictions.eq(AttributeConstants.ITEMTYPE_ID, AttributeConstants.ITEMTYPE_ITEMSWANTED));
				criterionList.add(Restrictions.eq("status.id", 4));
				count = CommonFunctionDelegator.getCountByCriterion(criterionList, Items.class);
				myAccountCount.setItemWantedMyWanteds(count);

				///////////////////  Tender My Wanted /////////////////////////

				if(criterionList.size() > 0 ){
			    	criterionList.clear();
			    }
				count = 0;
				loginUser = SessionUtil.getUser(request);
				criterionList.add(Restrictions.eq(AdminConstants.USERID , loginUser));
				criterionList.add(Restrictions.eq(AttributeConstants.ITEMTYPE_ID, 7));
				criterionList.add(Restrictions.eq("status.id", 4));
				count = CommonFunctionDelegator.getCountByCriterion(criterionList, Items.class);
				myAccountCount.setTenderMyWanteds(count);

				/////////////////// End Tender My Wanted /////////////////////////

				searchListCriteria.removeAllAttribute();
				searchListCriteria.setAttribute("user.id", new Object[]{user.getId(), SearchListCriteria.EQ});
				List<ItemsWatchlist> watchList = ItemsWatchListServiceDelegator.getList(searchListCriteria);
				ItemWantedBestOffer itemWantedBestOffer = new ItemWantedBestOffer();
				List<Items> itemsListTemp = new ArrayList<Items>();
				if(watchList != null){
					for(int i=0;i<watchList.size();i++){
						if(watchList.get(i).getItems().getItemtype().getId() == 4){
							Items items = ItemsServiceDelegator.getItemsById(watchList.get(i).getItems().getItemId());
							if(items.getStatus().getId() == 4){
								searchCriteria.removeAllAttribute();
								searchCriteria.setAttribute("itemId.itemId", items.getItemId());
								itemWantedBestOffer = (ItemWantedBestOffer) ((CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemWantedBestOffer.class)).size() > 0 ? CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemWantedBestOffer.class).get(AdminConstants.ZERO_NUMBER) : null);
								items.setItemWantedBestOffer(itemWantedBestOffer);
								itemsListTemp.add(items);
							}
						}
					}
				}

				if(itemsListTemp.size() > 0){
					myAccountCount.setItemWantedWatchingOthers(itemsListTemp.size());
				}

				///////////////////  Tender Watching Others /////////////////////////

				searchListCriteria.removeAllAttribute();
				searchListCriteria.setAttribute("user.id", new Object[]{user.getId(), SearchListCriteria.EQ});
				List<ItemsWatchlist> tenderWatchList = ItemsWatchListServiceDelegator.getList(searchListCriteria);
				ItemWantedBestOffer tenderBestOffer = new ItemWantedBestOffer();
				List<Items> tenderListTemp = new ArrayList<Items>();
				if(tenderWatchList != null){
					for(int i=0;i<tenderWatchList.size();i++){
						if(tenderWatchList.get(i).getItems().getItemtype().getId() == 7){
							Items items = ItemsServiceDelegator.getItemsById(tenderWatchList.get(i).getItems().getItemId());
							if(items.getStatus().getId() == 4){
								searchCriteria.removeAllAttribute();
								searchCriteria.setAttribute("itemId.itemId", items.getItemId());
								tenderBestOffer = (ItemWantedBestOffer) ((CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemWantedBestOffer.class)).size() > 0 ? CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemWantedBestOffer.class).get(AdminConstants.ZERO_NUMBER) : null);
								items.setItemWantedBestOffer(tenderBestOffer);
								tenderListTemp.add(items);
							}
						}
					}
				}

				if(tenderListTemp.size() > 0){
					myAccountCount.setTenderWatchingOthers(tenderListTemp.size());
				}

				///////////////////  Tender Watching Others /////////////////////////

				count = 0;
				loginUser = SessionUtil.getUser(request);

				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute("user", user);
				searchCriteria.setAttribute("status.id", 4);

				List<ItemOffers> itemOffersTemp = (List<ItemOffers>) CommonFunctionDelegator.GeneralgetCriteriaList(ItemOffers.class.getName(), searchCriteria);

				List<ItemOffers> itemWantedOffers = new ArrayList<ItemOffers>();

				for (int i = 0; i < itemOffersTemp.size(); i++) {
					if(itemOffersTemp.get(i).getItems().getItemtype().getId() == 4){
						itemWantedOffers.add(itemOffersTemp.get(i));
					}
				}

				if(itemWantedOffers.size() > 0){
					count = itemWantedOffers.size();
					myAccountCount.setItemWantedMyOffers(count);
				}

				///////////////////// Tender offers Count /////////////////////////////

				count = 0;
				loginUser = SessionUtil.getUser(request);

				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute("user", user);
				searchCriteria.setAttribute("status.id", 4);

				List<ItemOffers> itemTenderTemp = (List<ItemOffers>) CommonFunctionDelegator.GeneralgetCriteriaList(ItemOffers.class.getName(), searchCriteria);

				List<ItemOffers> itemTenderOffers = new ArrayList<ItemOffers>();

				for (int i = 0; i < itemTenderTemp.size(); i++) {
					if(itemTenderTemp.get(i).getItems().getItemtype().getId() == 4){
						itemTenderOffers.add(itemTenderTemp.get(i));
					}
				}

				if(itemTenderOffers.size() > 0){
					count = itemTenderOffers.size();
					myAccountCount.setTenderMyOffers(count);
				}

				///////////////////// End Tender offers Count /////////////////////////////

				searchListCriteria.removeAllAttribute();
				searchListCriteria.setAttribute("user", new Object[]{user, SearchListCriteria.NE});
				List<AuctionWonlist> purchased = new ArrayList<AuctionWonlist>();
				try{
					List<AuctionWonlist> list = AuctionServiceDelegator.getAuctionWonlistListBySearchListCriteria(searchListCriteria);
					for (int i=0;i<list.size();i++) {
						if((list.get(i).getItems().getItemtype().getId() == 4)){
							int id = list.get(i).getItems().getUserId().getId();
							if(id == user.getId()){
								purchased.add(list.get(i));
							}
						}
					}
				}catch (Exception e) {
					e.printStackTrace();
				}

				if(purchased.size() > 0){
					myAccountCount.setItemWantedPurchased(purchased.size());
				}

				///////////////////// Tender Purchased Count /////////////////////////////

				searchListCriteria.removeAllAttribute();
				searchListCriteria.setAttribute("user", new Object[]{user, SearchListCriteria.NE});
				List<AuctionWonlist> tenderPurchased = new ArrayList<AuctionWonlist>();
				try{
					List<AuctionWonlist> tenderPurchasedList = AuctionServiceDelegator.getAuctionWonlistListBySearchListCriteria(searchListCriteria);
					for (int i=0;i<tenderPurchasedList.size();i++) {
						if((tenderPurchasedList.get(i).getItems().getItemtype().getId() == 7)){
							int id = tenderPurchasedList.get(i).getItems().getUserId().getId();
							if(id == user.getId()){
								tenderPurchased.add(tenderPurchasedList.get(i));
							}
						}
					}
				}catch (Exception e) {
					e.printStackTrace();
				}

				if(tenderPurchased.size() > 0){
					myAccountCount.setTenderPurchased(tenderPurchased.size());
				}

				///////////////////// End Tender Purchased Count /////////////////////////////

				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute("user", user);
				List<AuctionWonlist> sold = new ArrayList<AuctionWonlist>();
				try{
					List<AuctionWonlist> list = (List<AuctionWonlist>) CommonFunctionDelegator.GeneralgetCriteriaList(AuctionWonlist.class.getName(), searchCriteria);
					for (int i=0;i<list.size();i++) {
						if(list.get(i).getItems().getItemtype().getId() == 4){
							sold.add(list.get(i));
						}
					}
				}catch (Exception e) {
					e.printStackTrace();
				}

				if(sold.size() > 0){
					myAccountCount.setItemWantedSold(sold.size());
				}

				request.getSession().setAttribute(AttributeConstants.MYACCOUNTCOUNT, myAccountCount);

				///////////////////// Tender Sold Count /////////////////////////////

				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute("user", user);
				List<AuctionWonlist> tenderSoldList = new ArrayList<AuctionWonlist>();
				try{
					List<AuctionWonlist> tenderSold = (List<AuctionWonlist>) CommonFunctionDelegator.GeneralgetCriteriaList(AuctionWonlist.class.getName(), searchCriteria);
					for (int i=0;i<tenderSold.size();i++) {
						if(tenderSold.get(i).getItems().getItemtype().getId() == 7){
							tenderSoldList.add(tenderSold.get(i));
						}
					}
				}catch (Exception e) {
					e.printStackTrace();
				}

				if(tenderSoldList.size() > 0){
					myAccountCount.setTenderSold(tenderSoldList.size());
				}

				request.getSession().setAttribute(AttributeConstants.MYACCOUNTCOUNT, myAccountCount);

				///////////////////// End Tender Sold Count /////////////////////////////


				if (remember) {
					Cookie userCookie = new Cookie(AttributeConstants.USER_ID, userName);
					Cookie passCookie = new Cookie(AttributeConstants.PASSWORD, CommonUtil.encrypt(password));
					userCookie.setMaxAge(31536000); // 60 * 60 * 24=86400 (1 day) *
					passCookie.setMaxAge(31536000);						// 365
					response.addCookie(userCookie);
					response.addCookie(passCookie);
				}

				if (logger.isDebugEnabled()) {
					logger.debug("execute() - end"); //$NON-NLS-1$
				}

				if(session.get("securedPage") != null) {
					this.redirectUri = session.get("securedPage").toString();
					Map parameters = (Map) request.getSession().getAttribute("securedPageParams");

					String requestString = "";
					Iterator parametersItr = parameters.entrySet().iterator();
			    	while (parametersItr.hasNext()) {
						Map.Entry entry = (Map.Entry) parametersItr.next();

						// add the parameter onto the request string
						Object parameterKey = entry.getKey();
						String[] parameterValue = (String[])entry.getValue();
						for(int j = 0; j < parameterValue.length; j++){
							// if it's the first parameter add a '?' else add a '&'
							requestString += (count == 0) ? "?" : "&";

							//get the parameter at this point
							requestString += parameterKey + "=" + parameterValue[j].toString();
						}
						count++;
					}

			    	this.redirectUri += requestString;
					session.remove("securedPage");
					return "redirectSuccess";
				}
				else {
					return SUCCESS;
				}
			} else {
				addActionError("Your account is Suspended.");
			}
		} else {
			addActionError("Invalid user name or password! Please try again!");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return INPUT;
	}

	/**
	 * Logout.
	 *
	 * @return the string
	 *
	 * @throws Exception
	 *             the exception
	 */
	@SuppressWarnings("unchecked")
	public String logout() throws Exception {
		Map<Object, Object> session = ActionContext.getContext().getSession();
		session.put(AttributeConstants.USER, null);
		request.getSession().setAttribute(AdminConstants.TEXT, null);
		request.getSession().setAttribute(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE, null);
		Cookie[] cookie = request.getCookies();

		for (int index = 0; index < cookie.length; index++) {
			if(cookie[index].getName().equals(AttributeConstants.USER_ID) || cookie[index].getName().equals(AttributeConstants.PASSWORD)) {
				cookie[index].setMaxAge( 0 );
				response.addCookie(cookie[index]);
			}
		}

		return SUCCESS;
	}

	/**
	 * Forgot user id.
	 *
	 * @return the string
	 *
	 * @throws Exception
	 *             the exception
	 */
	public String forgotUserId() throws Exception {
		this.userId = "true".intern();
		return SUCCESS;
	}

	/**
	 * Send user id.
	 *
	 * @return the string
	 *
	 * @throws Exception
	 *             the exception
	 */
	public String sendUserId() throws Exception {
//		if(Validator.isMailAddress(email)) {
			this.message = true;
			try{
			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute(AttributeConstants.EMAIL, email);
			List<User> userList = UserServiceDelegator.getList(searchCriteria);

			if(userList.size() > 0) {
				User user = userList.get(0);

				MailingSystem mailingSystem = (MailingSystem) ActionContext.getContext().getApplication().get(AdminConstants.MAILING_OPTIONS);
				MailHelper mailHelper = new MailHelper(mailingSystem.getServerAddress(), mailingSystem.getServerUsername(), mailingSystem.getServerPassword());

				StringBuffer URL = new StringBuffer();
				URL.append(CommonUtil.getLocalBasePath(request));
				URL.append("/mail/forgotUserId.htm?id=").append(user.getUserId()).append("&uname=").append(user.getFirstName()).append("%20").append(user.getLastName());

				String message = CommonUtil.getURLContent(URL.toString());
				mailHelper.sendMail(mailingSystem.getWebmasterEmailAddress(), user.getEmail(), "PrintPackPaper UserId...", message);

				addActionMessage("Your User Id Mail to your Email Address");
			} else {
				addActionMessage("Email address Not Found! <br>"+
					"Sorry, the email address you entered is not registered on our system. Please use the same email address you registered with on our system.");
			}
			}catch (Exception e) {
				addActionError("Please, try again later, your userId not sent to your mail-id");
				return INPUT;
			}
			/*	} else {
			this.userId = "true".intern();
			addActionError("Invalid e-mail address");
		}*/

		return SUCCESS;
	}

	/**
	 * Forgot password.
	 *
	 * @return the string
	 *
	 * @throws Exception
	 *             the exception
	 */
	public String forgotPassword() throws Exception {
		this.password = "true".intern();
		return SUCCESS;
	}

	/**
	 * Send password.
	 *
	 * @return the string
	 *
	 * @throws Exception
	 *             the exception
	 */
	public String sendPassword() throws Exception {
		this.message = true;
		try{
		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.setAttribute(AttributeConstants.USER_ID, userId);
		List<User> userList = UserServiceDelegator.getList(searchCriteria);

		if(userList.size() > 0) {
			User user = userList.get(0);

			MailingSystem mailingSystem = (MailingSystem) ActionContext.getContext().getApplication().get(AdminConstants.MAILING_OPTIONS);
			MailHelper mailHelper = new MailHelper(mailingSystem.getServerAddress(), mailingSystem.getServerUsername(), mailingSystem.getServerPassword());

			StringBuffer URL = new StringBuffer();
			URL.append(CommonUtil.getLocalBasePath(request));
			URL.append("/mail/forgotPassword.htm?pass=").append(user.getPassword()).append("&uname=").append(user.getFirstName()).append("%20").append(user.getLastName());

			String message = CommonUtil.getURLContent(URL.toString());
			mailHelper.sendMail(mailingSystem.getWebmasterEmailAddress(), user.getEmail(), "PrintPackPaper", message);

			addActionMessage("Your Password Mail to your Emial Address");
		} else {
			addActionMessage("User ID Not Found! <br>"+
					"Sorry, the User ID you entered is not registered on our system. Please use the same User ID you registered with on our system.");
		}
		}catch (Exception e) {
			addActionError("Please, try again later, your password not sent to your mail-id");
			return INPUT;
		}
		return SUCCESS;
	}

	/**
	 * Activate user by activation id.
	 *
	 * @return the string
	 */
	public String activateUserByActivationId() {
		if (logger.isDebugEnabled()) {
			logger.debug("activateUserByActivationId() - start"); //$NON-NLS-1$
		}

		if (this.request.getParameter(AttributeConstants.ACTIVATION_CODE) != null) {
			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute(AttributeConstants.ACTIVATION_CODE,
					this.request
							.getParameter(AttributeConstants.ACTIVATION_CODE));
			List<User> userList = UserServiceDelegator.getList(searchCriteria);

			if (userList.size() > 0) {
				User user = userList.get(0);
				user.setActive(true);
				user.setActivationKey(null);
				UserServiceDelegator.updateUser(user);
				this.userName = user.getUserId();
				this.password = user.getPassword();

				if (logger.isDebugEnabled()) {
					logger.debug("activateUserByActivationId() - end"); //$NON-NLS-1$
				}
				return SUCCESS;
			} else {
				if (logger.isDebugEnabled()) {
					logger.debug("activateUserByActivationId() - end"); //$NON-NLS-1$
				}
				return ERROR;
			}
		} else {
			if (logger.isDebugEnabled()) {
				logger.debug("activateUserByActivationId() - end"); //$NON-NLS-1$
			}
			return AttributeConstants.LOGIN;
		}
	}

	/** The username. */
	private String userName = null;

	/** The password. */
	private String password = null;

	private Map cookieMap;

	/**
	 * Gets the username.
	 *
	 * @return the username
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * Sets the username.
	 *
	 * @param userName
	 *            the user name
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * Gets the password.
	 *
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * Sets the password.
	 *
	 * @param password
	 *            the new password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
	 */
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	/**
	 * Gets the email.
	 *
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * Sets the email.
	 *
	 * @param email
	 *            the new email
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * Gets the user id.
	 *
	 * @return the user id
	 */
	public String getUserId() {
		return userId;
	}

	/**
	 * Sets the user id.
	 *
	 * @param userId
	 *            the new user id
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}

	/**
	 * Checks if is message.
	 *
	 * @return true, if is message
	 */
	public boolean isMessage() {
		return message;
	}

	/**
	 * Sets the message.
	 *
	 * @param message
	 *            the new message
	 */
	public void setMessage(boolean message) {
		this.message = message;
	}

	public boolean isRemember() {
		return remember;
	}

	public void setRemember(boolean remember) {
		this.remember = remember;
	}

	public boolean isForget() {
		return forget;
	}

	public void setForget(boolean forget) {
		this.forget = forget;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getRedirectUri() {
		return redirectUri;
	}

	public MyAccountCount getMyAccountCount() {
		return myAccountCount;
	}

	public void setMyAccountCount(MyAccountCount myAccountCount) {
		this.myAccountCount = myAccountCount;
	}
}